class Book:
    pass


class BookCollection:

    def __init__(self):
        self.data = ['book1','book2','book3']
        self.cur = 0

    # 类实现如下方法变成迭代器
    def __iter__(self):
        return self

    def __next__(self):
        if self.cur >= len(self.data):
            raise StopIteration() #抛出异常
        book = self.data[self.cur]
        self.cur += 1
        return book


books = BookCollection()
for book in books:
    print(book)

#一次性
for book in books:
    print(book)